package sanphone.com.runningschool.MyOrder;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

import sanphone.com.runningschool.BaseActivity;
import sanphone.com.runningschool.ForceOffline;
import sanphone.com.runningschool.Fragment.AcceptedMessageFragment;
import sanphone.com.runningschool.Fragment.SendMessageFragment;
import sanphone.com.runningschool.R;

public class MyOrderActivity extends BaseActivity implements View.OnClickListener {

    private Toolbar toolbar;
    private TextView send_order;
    private TextView accept_order;
    private ViewPager message_viewpager;
    private Fragment[] fragments=new Fragment[]{
            SendMessageFragment.getInstance(),
            AcceptedMessageFragment.getInstance()
    };
    private SharedPreferences sprf;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_order);
        sprf=getSharedPreferences("data",MODE_PRIVATE);
        ForceOffline offline=new ForceOffline(sprf.getString("schoolId",null),this);
        offline.check();
        initView();
        send_order.setOnClickListener(this);
        accept_order.setOnClickListener(this);


    }
    @SuppressLint("ResourceAsColor")
    private void initView() {
        toolbar =  findViewById(R.id.toolbar);
        send_order =  findViewById(R.id.send_order);
        accept_order =  findViewById(R.id.accept_order);
        message_viewpager = findViewById(R.id.message_viewpager);
        toolbar =  findViewById(R.id.toolbar);
        toolbar.setTitle("");
        setSupportActionBar(toolbar);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
        }

        message_viewpager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return fragments[position];
            }

            @Override
            public int getCount() {
                return fragments.length;
            }
        });
        message_viewpager.setCurrentItem(0);
        accept_order.setBackgroundColor(R.color.txt_click_background);
        send_order.setBackgroundColor(Color.WHITE);
        message_viewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @SuppressLint("ResourceAsColor")
            @Override
            public void onPageSelected(int position) {
                if(position==1){
                   send_order.setBackgroundColor(R.color.txt_click_background);
                   accept_order.setBackgroundColor(Color.WHITE);
                }
                else{
                    accept_order.setBackgroundColor(R.color.txt_click_background);
                    send_order.setBackgroundColor(Color.WHITE);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case android.R.id.home:
                MyOrderActivity.this.finish();
                break;
        }
        return true;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.accept_order:
                message_viewpager.setCurrentItem(1);
                accept_order.setBackgroundColor(Color.WHITE);
                break;
            case R.id.send_order:
                message_viewpager.setCurrentItem(0);
               send_order.setBackgroundColor(Color.WHITE);
                break;

        }



    }
}
